<?php
namespace app\api\controller;

use app\home\model\ShortLink;
use app\home\model\Supply as ModelSupply;
use app\home\model\SupplyImg;
use think\facade\Db;

class Supply
{
    public function index()
    {
        $page = request()->param('page') - 1;
        $page *= 10;
        $sql = "SELECT id FROM stpc_qzz_provider ORDER BY id DESC LIMIT $page, 1";
        $last_supply_id = Db::query($sql)[0]['id'];
        $supplies = ModelSupply::where('id', '<=', $last_supply_id)->where('name', '<>', '')->order('id desc')->limit(0, 10)->select();
        return json([
            'status'=>200,
            'supply'=>$supplies,
            'last_id'=>$last_supply_id,
            'count'=>ModelSupply::count(),
        ]);
    }
    public function edit(){
        $data = request()->param();
        $supply = ModelSupply::find($data['id']);
        $supply->save($data);
        return json([
            'supply'=> ModelSupply::find($supply->id)
        ]);
    }

    public function add(){
        $data = request()->param();
        $data['hash'] = get_hash();
        $data['add_time'] = date('Y-m-d h:i:s', time());
        $data['update_time'] = date('Y-m-d i:h:s');
        //supply parm
        $supply = new ModelSupply;
        $supply->save($data);
        $id = $supply->id;
        unset($supply);
        $supply = ModelSupply::find($id);
        $hash = $data['hash'];

        $sl = new ShortLink;
        $sl->save([
            'c_name'=>'Spd',
            'hash_str' => $hash,
            'p_name' => 'page',
            'p_id'=>$id,
        ]);


        // //upload file
        $oss = Upload::getOss();
        $bucket = 'jr1test';
        $files = [];

        foreach($_FILES as $file){
            $fileName = "supply/$hash/".Upload::getFileInfo($file['name'])['file'];
            $res = $oss->uploadFile($bucket, $fileName, $file['tmp_name'])['info'];
            array_push($files, $res['url']);
            $supply_img = new SupplyImg;
            $supply_img->save([
                'provid_id'=>$id,
                'img'=>$res['url'],
                'type'=>'supply'
            ]);
        }
        if (count($files) == 0){
            array_push($files, 'http://jr1test.oss-cn-beijing.aliyuncs.com/supply/pxb0H8/a2dbd303e3bc33cf9d19a31a68b17434.png');
        }
        $supply->save([
            'thumb'=>$files[0]
        ]);
        
        return json([
            'supply'=>$supply,
            'img'=>SupplyImg::where('provid_id', $id)->select(),
        ]);
    }

    public function del(){
        $id = request()->param('id');
        $hash = ModelSupply::find($id)['hash'];
        // return json($hash);
        ShortLink::where('hash_str', $hash)->delete();
        SupplyImg::where('provid_id', $id)->delete();
        ModelSupply::destroy($id);
    }
    public function getSupply()
    {
        $id = request()->param('id');
        $supply = ModelSupply::find($id);
        $temp_img_list = SupplyImg::where('provid_id', $id)->select();
        $img_list = [];
        foreach($temp_img_list as  $i){
            $img = [
                'name' => $i['id'],
                'url'   => $i['img'],
            ];
            array_push($img_list, $img);
        }
        $supply['img_list'] = $img_list;
        return json([
            'supply'=>$supply
        ]);
    }
    public function removeDetailImg(){
        $id = request()->param('id');
        return json(SupplyImg::destroy($id));
    }
}
